package org.dromara.system.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jakarta.servlet.http.HttpServletRequest;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.RandomUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.*;
import org.dromara.system.domain.bo.AppOrderSureBo;
import org.dromara.system.domain.bo.WxReq;
import org.dromara.system.domain.vo.AppProcurementPlanVo;
import org.dromara.system.mapper.*;
import org.dromara.system.pay.WxOpenIdUtil;
import org.dromara.system.pay.WxPayJsapiCertEndService;
import org.dromara.system.service.*;
import org.springframework.stereotype.Service;
import org.dromara.system.domain.bo.AppOrderBo;
import org.dromara.system.domain.vo.AppOrderVo;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.BufferedReader;
import java.math.BigDecimal;
import java.util.*;

import static org.dromara.system.pay.WxPayCertService.doXMLParse;
import static org.dromara.system.pay.WxPayService.PARTNERKEY;
import static org.dromara.system.pay.WxPayService.getSign;

/**
 * 订单Service业务层处理
 *
 * @author Lion Li
 * @date 2025-09-12
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AppOrderServiceImpl implements IAppOrderService {

    private final AppOrderMapper baseMapper;
    private final IAppProcurementDetailService appProcurementDetailService;
    private final IAppProcurementPlanService appProcurementPlanService;
    private final IAppCategoryService appCategoryService;
    private final IAppMaterialService appMaterialService;
    private final AppSupplierDetailMapper appSupplierDetailMapper;
    private final SysTenantMapper sysTenantMapper;
    private final AppDriverDetailMapper appDriverDetailMapper;
    private final AppWarehouseRecordMapper appWarehouseRecordMapper;
    private final AppQualityInfoMapper appQualityInfoMapper;
    /**
     * 查询订单
     *
     * @param id 主键
     * @return 订单
     */
    @Override
    public AppOrderVo queryById(String id){
        AppOrderVo appOrderVo = baseMapper.selectVoById(id);
        if (appOrderVo == null) {
            LambdaQueryWrapper<AppOrder> appOrderLambdaQueryWrapper = new LambdaQueryWrapper<>();
            appOrderLambdaQueryWrapper.eq(AppOrder::getOrderId,id);
            AppOrder appOrder = baseMapper.selectOne(appOrderLambdaQueryWrapper);
            appOrderVo = MapstructUtils.convert(appOrder, AppOrderVo.class);
            if (appOrderVo == null){
                return null;
            }
        }
        //设置采购详情
        Long planId = appOrderVo.getPlanId();
        AppProcurementPlanVo appProcurementPlanVo = appProcurementPlanService.queryById(planId);
        List<AppProcurementDetail> appProcurementDetails = appProcurementDetailService.queryListByPlanCode(appProcurementPlanVo.getPlanCode());
        if (!CollUtil.isEmpty(appProcurementDetails)){
            Map<Long, String> categoryMap = appCategoryService.selAllMap();
            Map<Long, String> materialMap = appMaterialService.selAllMap();
            appProcurementDetails.forEach(appProcurementDetail -> {
                appProcurementDetail.setCategoryName(categoryMap.get(appProcurementDetail.getCategoryId()));
                appProcurementDetail.setMaterialName(materialMap.get(appProcurementDetail.getMaterialTypeId()));
            });
            appOrderVo.setDetailList(appProcurementDetails);
        }
        //查询公司名称 根据供应商代号
        LambdaQueryWrapper<AppSupplierDetail> appOrderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        appOrderLambdaQueryWrapper.eq(AppSupplierDetail::getSupplierCode,appOrderVo.getSupplierCode());
        AppSupplierDetail appSupplierDetail = appSupplierDetailMapper.selectOne(appOrderLambdaQueryWrapper);
        if (appSupplierDetail != null){
            appOrderVo.setSupplierName(appSupplierDetail.getSupplierName());
            LambdaQueryWrapper<SysTenant> sysTenantLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sysTenantLambdaQueryWrapper.eq(SysTenant::getTenantId,appSupplierDetail.getTenantId());
            SysTenant sysTenant = sysTenantMapper.selectOne(sysTenantLambdaQueryWrapper);
            appOrderVo.setCompanyName(sysTenant.getCompanyName());
        }else {
            appOrderVo.setCompanyName("未知");
        }

        //检查是否入库
        LambdaQueryWrapper<AppWarehouseRecord> appWarehouseRecordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        appWarehouseRecordLambdaQueryWrapper.eq(AppWarehouseRecord::getPlanCode,appOrderVo.getPlanId());
        List<AppWarehouseRecord> appWarehouseRecords = appWarehouseRecordMapper.selectList(appWarehouseRecordLambdaQueryWrapper);
        if (CollUtil.isNotEmpty(appWarehouseRecords)){
            appOrderVo.setWarehouseFlag(1);
        }else {
            appOrderVo.setWarehouseFlag(0);
        }

        //检查是否出库
        LambdaQueryWrapper<AppWarehouseRecord> aw = new LambdaQueryWrapper<>();
        aw.eq(AppWarehouseRecord::getPlanCode,appOrderVo.getPlanId());
        aw.eq(AppWarehouseRecord::getType,1);
        AppWarehouseRecord appWarehouseRecord1 = appWarehouseRecordMapper.selectOne(aw);
        if (appWarehouseRecord1 != null){
            appOrderVo.setWarehouseOutFlag(1);
        }else {
            appOrderVo.setWarehouseOutFlag(0);
        }

        //查看是否质检
        if (appOrderVo.getQualityId()!=null){
            LambdaQueryWrapper<AppQualityInfo> appQualityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
            appQualityInfoLambdaQueryWrapper.eq(AppQualityInfo::getId,appOrderVo.getQualityId());
            AppQualityInfo appQualityInfo = appQualityInfoMapper.selectOne(appQualityInfoLambdaQueryWrapper);
            if (appQualityInfo != null){
                appOrderVo.setQualityInfo(appQualityInfo);
            }
        }

        //查询是否有司机
        if (appOrderVo.getDriverId()!=null){
            LambdaQueryWrapper<AppDriverDetail> aq = new LambdaQueryWrapper<>();
            aq.eq(AppDriverDetail::getDriverPhone,appOrderVo.getDriverId());
            AppDriverDetail appDriverDetail = appDriverDetailMapper.selectOne(aq);
            if (appDriverDetail != null){
                //设置车牌号
                appOrderVo.setCarPlate(appDriverDetail.getCarPlate());
            }
        }
        appOrderVo.setPressCarFee(appOrderVo.getPressFee());
        return appOrderVo;
    }

    /**
     * 分页查询订单列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 订单分页列表
     */
    @Override
    public TableDataInfo<AppOrderVo> queryPageList(AppOrderBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppOrder> lqw = buildQueryWrapper(bo);
        if (bo.getOrderStatus()!=null){
            if (bo.getOrderStatus()==3){
                lqw.eq(AppOrder::getStayCarrier,1);
            }
        }

        Page<AppOrderVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        //查询公司名称
        result.getRecords().forEach(appOrderVo -> {
            LambdaQueryWrapper<SysTenant> sysTenantLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sysTenantLambdaQueryWrapper.eq(SysTenant::getTenantId,appOrderVo.getTenantId());
            SysTenant sysTenant = sysTenantMapper.selectOne(sysTenantLambdaQueryWrapper);
            if (sysTenant != null){
                appOrderVo.setCompanyName(sysTenant.getCompanyName());
            }

        });
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的订单列表
     *
     * @param bo 查询条件
     * @return 订单列表
     */
    @Override
    public List<AppOrderVo> queryList(AppOrderBo bo) {
        LambdaQueryWrapper<AppOrder> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppOrder> buildQueryWrapper(AppOrderBo bo) {
        Map<String, Object> params = bo.getParams();
        if (bo.getType()!=null){
            if (bo.getType()==1){
                bo.setOrderStatus(2L);
            }
            if (bo.getType()==2){
                //待承运 是本司机 接单了 未承运的状态
                bo.setDriverId(LoginHelper.getUserId());
                bo.setOrderStatus(3L);
            }
        }
        LambdaQueryWrapper<AppOrder> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(AppOrder::getId);
        lqw.eq(StringUtils.isNotBlank(bo.getOrderId()), AppOrder::getOrderId, bo.getOrderId());
        lqw.eq(bo.getOrderStatus() != null, AppOrder::getOrderStatus, bo.getOrderStatus());
        lqw.eq(bo.getPlanId() != null, AppOrder::getPlanId, bo.getPlanId());
        lqw.eq(StringUtils.isNotBlank(bo.getSupplierCode()), AppOrder::getSupplierCode, bo.getSupplierCode());
        lqw.eq(StringUtils.isNotBlank(bo.getDeliveryAddress()), AppOrder::getDeliveryAddress, bo.getDeliveryAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseLocation()), AppOrder::getWarehouseLocation, bo.getWarehouseLocation());
        lqw.eq(StringUtils.isNotBlank(bo.getReceiveAddress()), AppOrder::getReceiveAddress, bo.getReceiveAddress());
        lqw.eq(bo.getDriverSelectType() != null, AppOrder::getDriverSelectType, bo.getDriverSelectType());
        lqw.eq(bo.getIsPressCar() != null, AppOrder::getIsPressCar, bo.getIsPressCar());
        lqw.eq(bo.getPressCarFee() != null, AppOrder::getPressCarFee, bo.getPressCarFee());
        lqw.eq(StringUtils.isNotBlank(bo.getTransportCompany()), AppOrder::getTransportCompany, bo.getTransportCompany());
        lqw.eq(StringUtils.isNotBlank(bo.getTaxpayerId()), AppOrder::getTaxpayerId, bo.getTaxpayerId());
        lqw.like(StringUtils.isNotBlank(bo.getContactName()), AppOrder::getContactName, bo.getContactName());
        lqw.eq(StringUtils.isNotBlank(bo.getContactPhone()), AppOrder::getContactPhone, bo.getContactPhone());
        lqw.like(StringUtils.isNotBlank(bo.getDriverName()), AppOrder::getDriverName, bo.getDriverName());
        lqw.eq(StringUtils.isNotBlank(bo.getDriverIdCard()), AppOrder::getDriverIdCard, bo.getDriverIdCard());
        lqw.eq(StringUtils.isNotBlank(bo.getDriverPhone()), AppOrder::getDriverPhone, bo.getDriverPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getCarPlate()), AppOrder::getCarPlate, bo.getCarPlate());
        lqw.eq(StringUtils.isNotBlank(bo.getCarType()), AppOrder::getCarType, bo.getCarType());
        lqw.eq(bo.getCarLength() != null, AppOrder::getCarLength, bo.getCarLength());
        lqw.eq(bo.getLoadCapacity() != null, AppOrder::getLoadCapacity, bo.getLoadCapacity());
        lqw.eq(StringUtils.isNotBlank(bo.getPhotoLoadInfo()), AppOrder::getPhotoLoadInfo, bo.getPhotoLoadInfo());
        lqw.eq(StringUtils.isNotBlank(bo.getPhotoUnloadInfo()), AppOrder::getPhotoUnloadInfo, bo.getPhotoUnloadInfo());
        lqw.eq(bo.getTotalWeight() != null, AppOrder::getTotalWeight, bo.getTotalWeight());
        lqw.eq(bo.getTareWeightLoad() != null, AppOrder::getTareWeightLoad, bo.getTareWeightLoad());
        lqw.eq(bo.getMaterialLoadWeight() != null, AppOrder::getMaterialLoadWeight, bo.getMaterialLoadWeight());
        lqw.eq(bo.getWeighingTotalWeight() != null, AppOrder::getWeighingTotalWeight, bo.getWeighingTotalWeight());
        lqw.eq(bo.getTareWeightUnload() != null, AppOrder::getTareWeightUnload, bo.getTareWeightUnload());
        lqw.eq(bo.getMaterialUnloadWeight() != null, AppOrder::getMaterialUnloadWeight, bo.getMaterialUnloadWeight());
        lqw.eq(bo.getRoadLoss() != null, AppOrder::getRoadLoss, bo.getRoadLoss());
        lqw.eq(bo.getDeductWeight() != null, AppOrder::getDeductWeight, bo.getDeductWeight());
        lqw.eq(bo.getNetWeight() != null, AppOrder::getNetWeight, bo.getNetWeight());
        lqw.eq(bo.getPackageNum() != null, AppOrder::getPackageNum, bo.getPackageNum());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseCode()), AppOrder::getWarehouseCode, bo.getWarehouseCode());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseCarPlate()), AppOrder::getWarehouseCarPlate, bo.getWarehouseCarPlate());
        lqw.eq(StringUtils.isNotBlank(bo.getWarehouseAdmin()), AppOrder::getWarehouseAdmin, bo.getWarehouseAdmin());
        lqw.eq(bo.getWarehouseDate() != null, AppOrder::getWarehouseDate, bo.getWarehouseDate());
        lqw.eq(bo.getQualityId() != null, AppOrder::getQualityId, bo.getQualityId());
        lqw.eq(bo.getFreight() != null, AppOrder::getFreight, bo.getFreight());
        lqw.eq(bo.getPressFee() != null, AppOrder::getPressFee, bo.getPressFee());
        lqw.eq(bo.getUnitPrice() != null, AppOrder::getUnitPrice, bo.getUnitPrice());
        lqw.eq(bo.getLoanAmount() != null, AppOrder::getLoanAmount, bo.getLoanAmount());
        return lqw;
    }

    /**
     * 新增订单
     *
     * @param bo 订单
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(AppOrderBo bo) {
        AppOrder add = MapstructUtils.convert(bo, AppOrder.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改订单
     *
     * @param bo 订单
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(AppOrderBo bo) {
        AppOrder update = MapstructUtils.convert(bo, AppOrder.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppOrder entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除订单信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public AppOrderVo queryByIdForUpdate(String orderId) {
        return baseMapper.selectByIdForUpdate(orderId);
    }

    @Override
    public boolean updateGrabOrder(AppOrderVo lockedOrder) {
        LambdaUpdateWrapper<AppOrder> appOrderLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        appOrderLambdaUpdateWrapper.eq(AppOrder::getOrderId, lockedOrder.getOrderId());
        appOrderLambdaUpdateWrapper.set(AppOrder::getOrderStatus, 2L);
        appOrderLambdaUpdateWrapper.set(AppOrder::getDriverPhone, lockedOrder.getDriverPhone());
        appOrderLambdaUpdateWrapper.set(AppOrder::getDriverName, lockedOrder.getDriverName());
        appOrderLambdaUpdateWrapper.set(AppOrder::getDriverId, lockedOrder.getDriverId());
        appOrderLambdaUpdateWrapper.set(AppOrder::getDriverIdCard, lockedOrder.getDriverIdCard());
        appOrderLambdaUpdateWrapper.set(AppOrder::getCarPlate, lockedOrder.getCarPlate());
        appOrderLambdaUpdateWrapper.set(AppOrder::getCarType, lockedOrder.getCarType());
        appOrderLambdaUpdateWrapper.set(AppOrder::getCarLength, lockedOrder.getCarLength());
        appOrderLambdaUpdateWrapper.set(AppOrder::getLoadCapacity, lockedOrder.getLoadCapacity());
        appOrderLambdaUpdateWrapper.set(AppOrder::getPhotoLoadInfo, lockedOrder.getPhotoLoadInfo());
        appOrderLambdaUpdateWrapper.set(AppOrder::getPhotoUnloadInfo, lockedOrder.getPhotoUnloadInfo());
        appOrderLambdaUpdateWrapper.set(AppOrder::getTotalWeight, lockedOrder.getTotalWeight());
        appOrderLambdaUpdateWrapper.set(AppOrder::getTareWeightLoad, lockedOrder.getTareWeightLoad());
        appOrderLambdaUpdateWrapper.set(AppOrder::getMaterialLoadWeight, lockedOrder.getMaterialLoadWeight());
        return baseMapper.update(appOrderLambdaUpdateWrapper) > 0;
    }

    @Override
    public R<Void> sureOrder(AppOrderSureBo appOrderSureBo) {
        //确认订单
        LambdaQueryWrapper<AppOrder> ao = new LambdaQueryWrapper<>();
        ao.eq(AppOrder::getOrderId,appOrderSureBo.getOrderId());
        AppOrder appOrder = baseMapper.selectOne(ao);
        if (appOrder==null){
           return R.fail("该订单不存在");
        }
        appOrder.setRemark(appOrderSureBo.getRemark());
        appOrder.setOtherRemark(appOrderSureBo.getOtherRemark());
        appOrder.setWarehouseLocation(appOrderSureBo.getWareHouseAddress());
        appOrder.setPressFee(appOrderSureBo.getPressCarPrice());
        appOrder.setIsPressCar(appOrderSureBo.getPressCarFlag());
        appOrder.setDriverSelectType(appOrderSureBo.getColumnCommentFlag());
        appOrder.setReceiveAddress(appOrderSureBo.getReceiveAddress());
        //确认之后 订单变为待接单状态 如果存在司机则变为待发货状态
        appOrder.setOrderStatus(2L);
        //若选择司机
        if (appOrderSureBo.getColumnCommentFlag()==1){
            appOrder.setDriverId(appOrderSureBo.getDriverId());
            appOrder.setDriverName(appOrderSureBo.getDriverName());
            appOrder.setDriverPhone(appOrderSureBo.getDriverPhone());
            appOrder.setOrderStatus(2L);
        }
        baseMapper.updateById(appOrder);
        return R.ok();
    }

    @Override
    public Map<String, String> orderPay(WxReq wxReq) {
        String openId = WxOpenIdUtil.getOpenId(wxReq.getCode());
        //查询订单
        LambdaQueryWrapper<AppOrder> appOrderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        appOrderLambdaQueryWrapper.eq(AppOrder::getOrderId,wxReq.getOrderId());
        AppOrder appOrder = baseMapper.selectOne(appOrderLambdaQueryWrapper);
        if (appOrder==null){
            return new HashMap<>();
        }
        //计算总价
        appOrder.setUnitPrice(appOrder.getUnitPrice() == null ? BigDecimal.ZERO : appOrder.getUnitPrice());
        BigDecimal totalPrice = appOrder.getUnitPrice().multiply(new BigDecimal(appOrder.getTareWeightLoad() == null ? 0 : appOrder.getTareWeightLoad()));
        //运费
        BigDecimal freight = appOrder.getFreight() == null ? BigDecimal.ZERO : appOrder.getFreight();
        //押车费
        BigDecimal pressCarFee = appOrder.getPressFee() == null ? BigDecimal.ZERO : appOrder.getPressFee();
        //贷款金额
        BigDecimal loanAmount = appOrder.getLoanAmount() == null ? BigDecimal.ZERO : appOrder.getLoanAmount();
        //合集
        BigDecimal totalAmount = totalPrice.add(freight).add(pressCarFee).add(loanAmount);
        //获取HttpServletRequest
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Map<String,String> param = new HashMap<>();

        //生成随机订单号
        String orderNoId = RandomUtils.generateOrderNoId();
        appOrder.setOrderNo(orderNoId);
        baseMapper.updateById(appOrder);
        param.put("tradeNo",orderNoId);
        param.put("notifyUrl","http://119.45.234.190:8764/app/order/wxCallBack");
        param.put("openId",openId);
        param.put("amount",totalAmount.toString());
        Map<String, String> appPayment;
        try {
            appPayment = WxPayJsapiCertEndService.createAppPayment(param, request);
            log.info("微信支付结果:{}",appPayment);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }


        return appPayment;
    }

    @Override
    public String wxCallBack(HttpServletRequest request) {
        try {
            // 1. 读取微信回调的 XML 内容
            StringBuilder sb = new StringBuilder();
            BufferedReader reader = request.getReader();
            String line = "";
            while ((line = reader.readLine()) != null) {
                sb.append(line);
            }
            reader.close();
            String xmlData = sb.toString();
            log.info("微信支付回调 XML：{}", xmlData);

            // 2. 解析 XML 转 Map
            Map<String, String> notifyMap = doXMLParse(xmlData);

            // 3. 校验签名是否正确
            SortedMap<Object, Object> signParams = new TreeMap<>();
            for (Map.Entry<String, String> entry : notifyMap.entrySet()) {
                signParams.put(entry.getKey(), entry.getValue());
            }

            // 4. 获取订单信息
            String outTradeNo = notifyMap.get("out_trade_no");
            String totalFee = notifyMap.get("total_fee");  // 单位：分
            String resultCode = notifyMap.get("result_code");

            log.info("微信支付回调：outTradeNo={}, totalFee={}, resultCode={}",
                outTradeNo, totalFee, resultCode);

            // 5. 判断微信支付是否成功
            if ("SUCCESS".equals(resultCode)) {
                LambdaQueryWrapper<AppOrder> appOrderLambdaQueryWrapper = new LambdaQueryWrapper<>();
                appOrderLambdaQueryWrapper.eq(AppOrder::getOrderNo,outTradeNo);
                AppOrder appOrder = baseMapper.selectOne(appOrderLambdaQueryWrapper);
                appOrder.setOrderStatus(11L);
                baseMapper.updateById(appOrder);
                return "支付成功";
            }

            // TODO: ----------【关键一步：幂等处理】----------
            // 你需要判断订单是否已处理过，避免重复发货、重复加余额
            // if(orderService.isProcessed(outTradeNo)){
            //     return returnSuccess();
            // }

            // TODO: ----------【你的业务逻辑写这里】----------
            // 如：更新订单支付状态、加余额、发送消息等
            // orderService.updatePaid(outTradeNo, totalFee);

            // 6. 返回成功给微信（重要！否则微信会继续重试 8 次）
            return "success";

        } catch (Exception e) {
            log.error("微信回调异常：{}", e.getMessage());
            return "系统错误";
        }
    }


}
